package com.stone.persistent.recyclerview.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.stone.persistent.recyclerview.R;
import com.stone.persistent.recyclerview.extensions.ContextExtensions;



public class ViewPagerIndicator extends View {
    private ViewPager2 viewPager2;
    private Paint paint;

    private float largerWidth = 10.0F;
    private float smallerWidth = 10.0F;
    private float rectHeight = 4.0F;
    private float interval = 4.0F;
    private int dotsCount;
    private int selectedColor = Color.WHITE;
    private int normalColor = Color.parseColor("#afdddddd");


    public final void setViewPager2(@NonNull ViewPager2 viewPager2, int realCount) {
        this.dotsCount = realCount;
        this.viewPager2 = viewPager2;
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                ViewPagerIndicator.this.invalidate();
            }
        });


    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (this.dotsCount > 1) {
            float width = (this.smallerWidth + this.interval) * (float) (this.dotsCount - 1) + this.largerWidth;
            float height = this.rectHeight + (float) 1;
            this.setMeasuredDimension((int) width, (int) height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }

    }

    protected void onDraw( Canvas canvas) {
        super.onDraw(canvas);
        RecyclerView.Adapter adapter = viewPager2 != null ? viewPager2.getAdapter() : null;
        if (adapter != null) {
            float startX = 0.0F;
            int index = 0;
            int var5 = this.dotsCount;
            if (index <= var5) {
                while (true) {
                    int realIndex = viewPager2.getCurrentItem() % this.dotsCount;
                    float var12;
                    if (index == realIndex) {
                        this.paint.setColor(selectedColor);
                        var12 = this.largerWidth;
                    } else {
                        this.paint.setColor(normalColor);
                        var12 = this.smallerWidth;
                    }

                    float thisWidth = var12;
                    float fromY = 0.0F;
                    float toX = startX + thisWidth;
                    float toY = this.rectHeight;
                    canvas.drawRoundRect(startX, fromY, toX, toY, this.rectHeight / (float) 2, this.rectHeight / (float) 2, this.paint);
                    startX = toX + this.interval;
                    if (index == var5) {
                        break;
                    }

                    ++index;
                }
            }
        }

    }

    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        largerWidth = ContextExtensions.dp2px(context, 12.0F);
        smallerWidth = ContextExtensions.dp2px(context, 7.0F);
        rectHeight = ContextExtensions.dp2px(context, 2.0F);
        interval = ContextExtensions.dp2px(context, 4.0F);
        TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.indicator);
        normalColor = attr.getColor(R.styleable.indicator_normalColor, normalColor);
        selectedColor = attr.getColor(R.styleable.indicator_selectedColor, selectedColor);
        attr.recycle();
    }

    public ViewPagerIndicator( Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewPagerIndicator( Context context) {
        this(context, null);
    }

}
